package org.jxf.gps.jt808.server;

import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.frame.DelimiterBasedFrameDecoder;

public class Server {

	public static final ChannelGroup allChannels = new DefaultChannelGroup("server" );		
	
	public static void main(String[] args) throws Exception{
		
		
		//1、创建channelfactory
		ChannelFactory channelFactory = new NioServerSocketChannelFactory(Executors
				.newCachedThreadPool(), Executors.newCachedThreadPool());

		//2、根据channelfactory创建Server
		ServerBootstrap bootstrap = new ServerBootstrap(channelFactory);
		
		//3、通过Server取pipeline
		ChannelPipeline pipeline = bootstrap.getPipeline();
		
		//4、创建Handler
		ServerHandler handler = new ServerHandler();
		
		//5、为pipeline添加handler
		ChannelBuffer delimiter = ChannelBuffers.buffer(1);
		delimiter.writeByte(0x7E);
		pipeline.addLast("1", new DelimiterBasedFrameDecoder(
				Integer.MAX_VALUE,delimiter));
		pipeline.addLast("2", handler);

//        pipeline.addLast("decoder", new StringDecoder());
//        pipeline.addLast("encoder", new StringEncoder());
		
		//6、设置Server的参数
		bootstrap.setOption("child.tcpNoDelay", true);
		bootstrap.setOption("child.keepAlive", true);

		//7、绑定IP和端口，启动服务
		Channel channel = bootstrap.bind(new InetSocketAddress(59090));

	}
}
